home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / ust1a.arc / CRC16.C next >
Encoding:
C/C++ Source or Header  |  1985-09-05  |  2.9 KB  |  144 lines

  1. /*+
  2.  * File: crc16.c
  3.  * Description:
  4.  *  Computes crc for the named filenames.
  5.  *  Output has this formatt.
  6.  *      file=[i_file], size=[fsize], crc=[decval](dec)=[hexval](hex)
  7.  *  
  8.  * Usages:
  9.  *  crc16 <i_files>
  10.  *
  11.  *  Author: Mohsen Banan.
  12.  *
  13.  *  This program is public domain software, no warranty intended or
  14.  *  implied.
  15.  *  General permission to copy or modify, but not for profit,  is
  16.  *  hereby  granted.
  17.  *
  18.  * Functions:
  19.  *
  20.  *
  21.  * Audit Trail:  $Log:    crc16.c,v $
  22.  * Revision 1.1  85/08/28  08:38:12  mohsen
  23.  * original posting to the net
  24.  * 
  25.  * 
  26.  *
  27. -*/
  28.  
  29. #ifdef RCS_VER
  30. static char *rcs = "$Header: crc16.c,v 1.1 85/08/28 08:38:12 mohsen Exp $";
  31. #endif
  32.  
  33. /* #includes */
  34. #include "mbstd.h"
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include <fcntl.h>
  38. #ifdef unix
  39. #include "msc3.h"
  40. #endif
  41.  
  42. /* #defines */
  43.  
  44. /* external variables */
  45.  
  46. /* referenced external function declarations */
  47. EXTERN USHORT fcrc16();
  48. /* internal function declarations */
  49. PUBLIC VOID crc16();
  50. PUBLIC VOID cant_open();
  51. STATIC VOID usage();
  52.  
  53. /* global variables */
  54.  
  55. /* static variables */
  56. STATIC CHAR * prog_name;
  57.  
  58.  
  59. INT
  60. main (argc, argv)
  61. INT argc;
  62. CHAR * argv[];
  63. {
  64.     crc16(argc, argv);
  65. }
  66.     
  67.  
  68. /*<
  69.  * Function:crc16
  70.  * Description:
  71.  *
  72.  * Returns:
  73.  *  VOID
  74.  *  
  75. >*/
  76. PUBLIC VOID 
  77. crc16 (argc,argv)
  78. INT argc;
  79. CHAR * argv[];
  80. {
  81.     FILE * i_fp;
  82.     FILE * o_fp;
  83.  
  84.     INT i;
  85.     USHORT crc;
  86.     LONG fsize ;
  87.  
  88.     i_fp = stdin;
  89.     o_fp = stdout;
  90.     prog_name = argv[0];
  91.     for (i=1; i<argc; ++i) {
  92.         if (*argv[i] == '-') {
  93.             /* To handle concatenated switches */
  94.             INT j;
  95.             j=i;
  96.             while (*(++argv[j])) {
  97.                 switch (*argv[j]) {
  98.                 case 'o':
  99.                 case 'O':
  100.                     if ( !(o_fp = fopen(argv[++i], "w")) ) {
  101.                         cant_open (prog_name, argv[i]);
  102.                         exit (1);
  103.                     }
  104.                     break;
  105.                 default:
  106.                     usage();
  107.                     exit(1);
  108.                 } /* switch (*argv[j]) */
  109.             } /* while (*(++argv[j])) */
  110.         } /* if '-' */
  111.         else {
  112.             if (!(i_fp = fopen (argv[i], "r"))) {
  113.                 cant_open (prog_name, argv[i]);
  114.                 exit (1);
  115.             } 
  116.             setmode (fileno(i_fp), O_BINARY);
  117.             setmode (fileno(o_fp), O_TEXT);
  118.             crc = fcrc16 (i_fp, 0, &fsize);
  119.             fprintf (o_fp, "file=%s, size=%ld, crc=%u(dec)=%x(hex)\n",
  120.                     argv[i], fsize, crc, crc);
  121.             fclose (i_fp);
  122.         }
  123.     } /* for i<argc */
  124.     fclose (o_fp);
  125.     exit (0);
  126. }
  127.  
  128. PUBLIC VOID
  129. cant_open (prog_name,filename)
  130. CHAR * prog_name;
  131. CHAR * filename;
  132. {
  133.     fprintf (stderr, "%s :can not open %s \n", prog_name, filename);
  134.  
  135. STATIC VOID
  136. usage ()
  137. {
  138.     fprintf (stderr, "Usage: %s <i_files> \n", prog_name);
  139. }
  140.  
  141.  
  142.  
  143.